 //
//  CodeViewController.m
//  dyseller
//
//  Created by 红喇叭 on 2017/8/14.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "CodeViewController.h"
#import "PayOrderViewController.h"

@interface CodeViewController ()<UITextFieldDelegate>{
    UIButton *sureBtn;
}

@property (strong, nonatomic)  UITextField *codeText;


@end

@implementation CodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavTitle:@"输入验证码" titleColor:[UIColor blackColor] font:font17];
    
    
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    leftView.backgroundColor = [UIColor clearColor];
    __weak typeof(self) weakself = self;
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 44,44);
    [backBtn setImage:[UIImage imageNamed:@"caiwu_nav_icon_default.png"] forState:UIControlStateNormal];
    [backBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [weakself.navigationController popViewControllerAnimated:YES];
    }];
    [leftView addSubview:backBtn];
    
    UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(40, 0, 40, 44)];
    [close setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [close setTitle:@"关闭" forState:UIControlStateNormal];
    [close setBackgroundColor:[UIColor clearColor]];
    [close handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [weakself.navigationController popToRootViewControllerAnimated:YES];
    }];
    [leftView addSubview:close];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftView];
    
    
    _codeText = [[UITextField alloc] init];
    

    _codeText.placeholder = @"请输入验证编码";
    _codeText.clearButtonMode = UITextFieldViewModeWhileEditing;
    _codeText.backgroundColor = [UIColor whiteColor];
    _codeText.returnKeyType = UIReturnKeyDone;
    _codeText.delegate = self;
    [self.view addSubview:_codeText];
    _codeText.sd_layout.topSpaceToView(self.view, 19).widthIs(GETWITH(690)).heightIs(GETHEIGHT(88)).centerXEqualToView(self.view);
    
    [self.codeText addTarget:self action:@selector(passConTextChange:) forControlEvents:UIControlEventEditingChanged];
    //注意：事件类型是：`UIControlEventEditingChanged`
    
    UILabel *prompt = [[UILabel alloc] init];
    prompt.text = @"请确认你输入正确的验证码";
   // prompt.textAlignment = NSTextAlignmentCenter;
    prompt.font = font14;
    
    prompt.textColor = UIColorFromRGB(0x666666);
    [self.view addSubview:prompt];
    prompt.sd_layout.topSpaceToView(_codeText, 30).widthIs(GETWITH(690)).autoHeightRatio(0).centerXEqualToView(self.view);
    
    sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
//    [sureBtn setBackgroundImage:[UIImage imageNamed:@"sanma_btn_default_selected.png"] forState:UIControlStateNormal];
    [sureBtn setBackgroundColor:UIColorFromRGB(0xcccccc)];
    sureBtn.layer.cornerRadius = 5;
    sureBtn.layer.masksToBounds = YES;
    
    [sureBtn addTarget:self action:@selector(sureCode:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureBtn];
    sureBtn.sd_layout.topSpaceToView(prompt, 30).widthIs(GETWITH(690)).heightIs(GETHEIGHT(88)).centerXEqualToView(self.view);
    
}
- (void)sureCode:(UIButton *)sender {
    
    if(_codeText.text.length<1){
       [AlertUtils showToastWithMessage:@"订单号不能为空" inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        return;
    }
 //   __weak typeof(self)weakself = self;
//    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Scan/orderVerify" parameter:@{@"order_sn":_codeText.text,@"flag":@"1"} successBlock:^(PPHttpRequest *request, id responseObj) {
//       _codeText.text = @"";
//         [AlertUtils showHudInView:self.view hint:@"核销成功" hideAfterDelay:1.0];
//    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
//         [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
//    }];
    __weak typeof(self)weakself = self;
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Scan/orderVerify" parameter:@{@"order_sn":_codeText.text} successBlock:^(PPHttpRequest *request, id responseObj) {
             _codeText.text = @"";
        [_codeText resignFirstResponder];
            PayOrderViewController *vc = [[PayOrderViewController alloc] initWith:responseObj style:2];
            vc.title = @"订单详情";
            [weakself.navigationController pushViewController:vc animated:YES];
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
         [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];


    
}


-(void)passConTextChange:(id)sender{
    UITextField* target=(UITextField*)sender;
    if(target.text.length >0){
        [sureBtn setBackgroundColor:UIColorFromRGB(0xe71a41)];
        //  _login.enabled = YES;
    }else{
        [sureBtn setBackgroundColor:UIColorFromRGB(0xcccccc)];
        //  _login.enabled = NO;
    }
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

@end
